<template>
  <div class="login-panel">
    <h1 class="title">后台管理系统</h1>
    <div class="tabs">
      <el-tabs type="border-card" v-model="activeName" stretch>
        <el-tab-pane label="账号登录" name="account">
          <template #label>
            <div class="label">
              <el-icon><UserFilled /></el-icon>
              <span class="text">账号登录</span>
            </div>
          </template>
          <panel-account ref="accountRef" />
        </el-tab-pane>
        <el-tab-pane label="手机登录" name="phone">
          <template #label>
            <div class="label">
              <el-icon><Cellphone /></el-icon>
              <span class="text">手机登录</span>
            </div>
          </template>
          <panel-phone />
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="controls">
      <el-checkbox v-model="isRemPwd" label="记住密码" size="large" />
      <el-link type="primary">忘记密码</el-link>
    </div>
    <el-button type="primary" class="login-btn" size="large" @click="handleLoginAction"
      >立即登录</el-button
    >
  </div>
</template>

<script setup lang="ts">
import { localCache } from '@/utils/cache'
import panelAccount from './panel-account.vue'
import panelPhone from './panel-phone.vue'
import { ref, watch } from 'vue'

const isRemPwd = ref<boolean>(localCache.getCache('isRemPwd') ?? false) // isRemPwd判断是否选中[记住密码]
watch(isRemPwd, (newValue) => {
  console.log(newValue, '==w')
  localCache.setCache('isRemPwd', JSON.parse(newValue))
})
const activeName = ref<string>('account')
// typeof PanelAccount = InstanceType<typeof PanelAccount>  ref获取子组件的ts写法
const accountRef = ref<InstanceType<typeof PanelAccount>>()

const handleLoginAction = () => {
  if (activeName.value === 'account') {
    console.log('账号登录')
    accountRef.value?.loginAction(isRemPwd.value)
    if (isRemPwd.value) {
      console.log('记住账号和密码')
    }
  } else {
    console.log('手机登录')
  }
}
</script>

<style scoped lang="less">
.login-panel {
  width: 330px;
  margin-bottom: 150px;

  .title {
    text-align: center;
    margin-bottom: 15px;
  }

  .icon {
    display: flex;
    align-items: center;
    justify-content: center;

    .text {
      margin-left: 5px;
    }
  }

  .controls {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
  }

  .login-btn {
    margin-top: 10px;
    width: 100%;
  }
}
</style>
